﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace RTC.Web
{
    public class RTCDbContext : DbContext
    {
        public RTCDbContext()
            : base("name=" + SmartSpace.Configuration.DefaultConnectionStringName)
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<RTCDbContext>(new MigrateDatabaseToLatestVersion<RTCDbContext, DBMC>());
            }
            else
            {
                Database.SetInitializer<RTCDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RTCMessage>().ToTable("RTC_Message");
            modelBuilder.Entity<RTCMessage>().HasKey(p => p.Id);

            modelBuilder.Entity<RTCReceiveRecord>().ToTable("RTC_ReceiveRecord");
            modelBuilder.Entity<RTCReceiveRecord>().HasKey(p => p.Account);

            modelBuilder.Entity<RTCUser>().ToTable("RTC_User");
            modelBuilder.Entity<RTCUser>().HasKey(p => p.Account);

            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class DBMC : DbMigrationsConfiguration<RTCDbContext>
    {
        public DBMC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}
